/** 校验格式是否正确 */

// 校验邮箱格式
function isEmail(str) {
	var re = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	if (!re.exec(str)) {
        return false;
	}
    else
       return true;
}

// 校验是否为汉字
function checkChinese(str) {
	var re = /^\s*$/g;
	if (!re.exec(str)) {
        return false;
	}
    else
       return true;
}

// 校验手机号是否正确
function checkMobile(str) {
	var re = /^(0|86|17951)?(13[0-9]|15[012356789]|17[01678]|18[0-9]|14[57])[0-9]{8}$/;
	if (!re.exec(str)) {
        return false;
	}
    else
       return true;
}

// 校验固定电话是否正确
function checkMobile2(str) {
	var re = /^(0[0-9]{2,3}\-)([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
	if (!re.exec(str)) {
        return false;
	}
    else
       return true;
}

// 校验图片格式
function checkPhoto(url) {
	var index = url.lastIndexOf('.');
	var photoType = url.substring(index+1);
	if (photoType != 'jpg' && photoType != 'png' && photoType != 'gif') {
		return false;
	}
	return true;
}

// 校验车牌号
function checkChinese(str) {
	var re = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
	if (!re.exec(str)) {
        return false;
	}
    else
       return true;
}

var tools = {
        /**
         * 判断是否为手机号码
         * @param strValue 校验的值
         * @returns {boolean}
         */
        isMobile: function(strValue) {
            if (!strValue)
                return false;
            var pattern = /^(13|14|15|17|18)[0-9]{9}$/;
            return this.executeExp(pattern, strValue);
        },
        /**
         * 验证身份证的有效性
         * @param strValueo 身份证ID
         * @returns {boolean}
         */
        isCardID: function(strValue) {
            if (!strValue) {
                return false;
            }
            strValue = strValue.toUpperCase();
            var vcity = {
                11: "北京",
                12: "天津",
                13: "河北",
                14: "山西",
                15: "内蒙古",
                21: "辽宁",
                22: "吉林",
                23: "黑龙江",
                31: "上海",
                32: "江苏",
                33: "浙江",
                34: "安徽",
                35: "福建",
                36: "江西",
                37: "山东",
                41: "河南",
                42: "湖北",
                43: "湖南",
                44: "广东",
                45: "广西",
                46: "海南",
                50: "重庆",
                51: "四川",
                52: "贵州",
                53: "云南",
                54: "西藏",
                61: "陕西",
                62: "甘肃",
                63: "青海",
                64: "宁夏",
                65: "新疆",
                71: "台湾",
                81: "香港",
                82: "澳门",
                91: "国外"
            };
            //校验长度，类型,身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
            var pattern = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
            if (tools.executeExp(pattern, strValue) === false) {
                return false;
            }
            //检查省份
            var province = strValue.substr(0, 2);
            if (vcity[province] == undefined) {
                return false;
            }
            //校验生日
            var len = strValue.length;
            //身份证15位时，次序为省（3位）市（3位）年（2位）月（2位）日（2位）校验位（3位），皆为数字
            if (len == 15) {
                var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
                var arr_data = strValue.match(re_fifteen);
                var year = parseInt('19' + arr_data[2]);
                var month = parseInt(arr_data[3]);
                var day = parseInt(arr_data[4]);
                var birthday = new Date('19' + year + '/' + month + '/' + day);
                //var birthday = new Date();
                birthday.setFullYear(year);
                birthday.setMonth(month - 1);
                birthday.setDate(day);
                var now = new Date();
                var now_year = now.getFullYear();
                //年月日是否合理
                if (birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) {
                    //判断年份的范围（3岁到100岁之间)
                    var time = now_year - year;
                    if (!(time >= 3 && time <= 100)) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
            //身份证18位时，次序为省（3位）市（3位）年（4位）月（2位）日（2位）校验位（4位），校验位末尾可能为X
            if (len == 18) {
                var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
                var arr_data = strValue.match(re_eighteen);
                var year = parseInt(arr_data[2]);
                var month = parseInt(arr_data[3]);
                var day = parseInt(arr_data[4]);
                var birthday = new Date(year + '/' + month + '/' + day);
                //var birthday = new Date();
                birthday.setFullYear(year);
                birthday.setMonth(month - 1);
                birthday.setDate(day);
                var now = new Date();
                var now_year = now.getFullYear();
                //年月日是否合理
                if (birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) {
                    //判断年份的范围（3岁到100岁之间)
                    var time = now_year - year;
                    if (!(time >= 3 && time <= 100)) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
            //检验位的检测
            //15位转18位
            if (strValue.length == 15) {
                var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                var cardTemp = 0,
                    i;
                strValue = strValue.substr(0, 6) + '19' + strValue.substr(6, strValue.length - 6);
                for (i = 0; i < 17; i++) {
                    cardTemp += strValue.substr(i, 1) * arrInt[i];
                }
                strValue += arrCh[cardTemp % 11];
            }
            if (strValue.length == 18) {
                var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                var cardTemp = 0,
                    i,
                    valnum;
                for (i = 0; i < 17; i++) {
                    cardTemp += parseInt(strValue.substr(i, 1)) * arrInt[i];
                }
                valnum = arrCh[cardTemp % 11];
                if (!(valnum == strValue.substr(17, 1))) {
                    return false;
                }
            }
            return true;
        },
         /**
         * 获取距当前时间多少天的日期
         * @param now 当前时间
         * @param addDayCount 天数
         * @param sign 分隔符
         * @returns 2016-10-10
         */
        getDateStr: function(now, addDayCount, sign) {
            var dd = new Date(now);
            dd.setDate(dd.getDate() + addDayCount * 1);
            //获取AddDayCount天后的日期

            var y = dd.getFullYear();
            var m = dd.getMonth() + 1;
            //获取当前月份的日期
            var d = dd.getDate();
            if (m < 10) {
                m = "0" + m;
            }
            if (d < 10) {
                d = "0" + d;
            }
            return y + sign + m + sign + d;
        },
        /**
         * 获取距当前月份
         * @param now 当前月份
         * @param addDayCount 天数
         * @param sign 分隔符
         * @returns 2016-10-10
         */
        getMonthStr: function(now, addMonthCount, sign) {
            var dd = new Date(now);
            dd.setMonth(dd.getMonth() - addMonthCount * 1);
            //获取AddDayCount天后的日期

            var y = dd.getFullYear();
            var m = dd.getMonth() + 1;
            //获取当前月份的日期
            if (m < 10) {
                m = "0" + m;
            }
            return y + sign + m;
        },
        /**
         * 判断字符串是否为空
         * @param strValue 校验的值
         * @returns {boolean}
         */
        isEmpty: function (strValue) {
            strValue = jQuery.trim(strValue);
            return strValue.length == 0;
        },
        /**
         * 判断字符串是否非空
         * @param strValue 校验的值
         * @returns {boolean}
         */
        isNotEmpty: function(strValue) {
            return !isEmpty(strValue);
        },
        /**
         * 执行正则表达式
         * @param pattern 校验的正则表达式
         * @param strValue 校验的值
         * @returns {boolean}
         */
        executeExp: function (pattern, strValue) {
            return pattern.test(strValue);
        },
        strToInt: function (strValue) {
            while (strValue.length > 1 && strValue.substring(0, 1) == "0") {
                strValue = strValue.substring(1, strValue.length);
            }
            return parseInt(strValue);
        },
        bankNoCheck: function(bankno){
			if (bankno.length < 16 || bankno.length > 19) {
			//$("#banknoInfo").html("银行卡号长度必须在16到19之间");
			return false;
			}
			var num = /^\d*$/;  //全数字
			if (!num.exec(bankno)) {
			//$("#banknoInfo").html("银行卡号必须全为数字");
				return false;
			}
			//开头6位
			var strBin="10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";    
			if (strBin.indexOf(bankno.substring(0, 2))== -1) {
			//$("#banknoInfo").html("银行卡号开头6位不符合规范");
				return false;
			}
	        var lastNum=bankno.substr(bankno.length-1,1);//取出最后一位（与luhm进行比较）
	    
	        var first15Num=bankno.substr(0,bankno.length-1);//前15或18位
	        var newArr=new Array();
	        for(var i=first15Num.length-1;i>-1;i--){    //前15或18位倒序存进数组
	            newArr.push(first15Num.substr(i,1));
	        }
	        var arrJiShu=new Array();  //奇数位*2的积 <9
	        var arrJiShu2=new Array(); //奇数位*2的积 >9
	        
	        var arrOuShu=new Array();  //偶数位数组
	        for(var j=0;j<newArr.length;j++){
	            if((j+1)%2==1){//奇数位
	                if(parseInt(newArr[j])*2<9)
	                	arrJiShu.push(parseInt(newArr[j])*2);
	                else
	                	arrJiShu2.push(parseInt(newArr[j])*2);
	            }
	            else //偶数位
	            	arrOuShu.push(newArr[j]);
	        }
	        
	        var jishu_child1=new Array();//奇数位*2 >9 的分割之后的数组个位数
	        var jishu_child2=new Array();//奇数位*2 >9 的分割之后的数组十位数
	        for(var h=0;h<arrJiShu2.length;h++){
	            jishu_child1.push(parseInt(arrJiShu2[h])%10);
	            jishu_child2.push(parseInt(arrJiShu2[h])/10);
	        }        
	        
	        var sumJiShu=0; //奇数位*2 < 9 的数组之和
	        var sumOuShu=0; //偶数位数组之和
	        var sumJiShuChild1=0; //奇数位*2 >9 的分割之后的数组个位数之和
	        var sumJiShuChild2=0; //奇数位*2 >9 的分割之后的数组十位数之和
	        var sumTotal=0;
	        for(var m=0;m<arrJiShu.length;m++){
	            sumJiShu=sumJiShu+parseInt(arrJiShu[m]);
	        }
	        
	        for(var n=0;n<arrOuShu.length;n++){
	            sumOuShu=sumOuShu+parseInt(arrOuShu[n]);
	        }
	        
	        for(var p=0;p<jishu_child1.length;p++){
	            sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);
	            sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);
	        }      
	        //计算总和
	        sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);
	        
	        //计算Luhm值
	        var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;        
	        var luhm= 10-k;
	        
	        if(lastNum==luhm){
	        	// $("#banknoInfo").html("Luhm验证通过");
	        	return true;
	        }
	        else{
		        // $("#banknoInfo").html("银行卡号必须符合Luhm校验");
		        return false;
        }        
    }
}